package com.fzy.core.tags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import com.fzy.core.entity.Page;

public class PageTag extends TagSupport {

	private static final long serialVersionUID = -774840953985543612L;
	
	String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int doEndTag() throws JspException{
		Page page = (Page)pageContext.getRequest().getAttribute(name);
		String html = "";
		if(page != null){
			long count = page.getTotalCount();
			if(count < 0) count = 0;
			html += "<input type=\"hidden\" name=\"page.orderBy\" class=\"orderBy\" value=\""+StringUtils.trimToEmpty(page.getOrderBy())+"\" />";
			html += "<input type=\"hidden\" name=\"page.order\" class=\"order\" value=\""+StringUtils.trimToEmpty(page.getOrder())+"\" />";
			html += "<table class=\"page\">";
			html += "	<tr>";
			html += "		<td class=\"page-message\">";
			html += "			<span>\u7B2C "+page.getPageNo()+" \u9875, \u5171 "+page.getTotalPages()+" \u9875, \u5171 "+count+" \u6761\u8BB0\u5F55, </span>";
			html += "			<span>\u6BCF\u9875\u663E\u793A </span>";
			html += "			<input type=\"text\" style=\"width:25px;\" name=\"page.pageSize\" class=\"pageSize\" value=\""+page.getPageSize()+"\" />";
			html += "			<span> \u6761\u8BB0\u5F55\u3002</span>";
			html += "		</td>";
			html += "		<td class=\"page-operation\">";
			html += "			<span class=\"link\" onclick=\"javascript:jumpPage(1,this)\">\u9996\u9875</span>";
			if(page.isHasPre())
			html += "			<span class=\"link\" onclick=\"javascript:jumpPage("+page.getPrePage()+",this)\">\u4E0A\u4E00\u9875</span>";
			if(page.isHasNext())
			html += "			<span class=\"link\" onclick=\"javascript:jumpPage("+page.getNextPage()+",this)\">\u4E0B\u4E00\u9875</span>";
			html += "			<span class=\"link\" onclick=\"javascript:jumpPage("+page.getTotalPages()+",this)\">\u672B\u9875</span>";
			html += "			<input type=\"text\" style=\"width:15px;\" name=\"page.pageNo\" class=\"pageNo\" value=\""+page.getPageNo()+"\" />";
			html += "			<input type=\"submit\" class=\"bbutton\" value=\"GO\" />";
			html += "		</td>";
			html += "	</tr>";
			html += "</table>";
		}else {
			html += "<input type=\"hidden\" name=\"pageNo\" class=\"pageNo\" value=\"1\" />";
			html += "<input type=\"hidden\" name=\"orderBy\" class=\"orderBy\" value=\"\" />";
			html += "<input type=\"hidden\" name=\"order\" class=\"order\" value=\"\" />";
		}
		try {
			pageContext.getOut().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}
}
